<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="项目名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="项目名称"></el-input>
    </el-form-item>
    <el-form-item label="是否为可申请项目0：不可申请  1：可申请" prop="useType">
      <el-input v-model="dataForm.useType" placeholder="是否为可申请项目0：不可申请  1：可申请"></el-input>
    </el-form-item>
    <el-form-item label="申请类型0单人1多人" prop="renType">
      <el-input v-model="dataForm.renType" placeholder="申请类型0单人1多人"></el-input>
    </el-form-item>
    <el-form-item label="父级id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父级id"></el-input>
    </el-form-item>
    <el-form-item label="层级" prop="level">
      <el-input v-model="dataForm.level" placeholder="层级"></el-input>
    </el-form-item>
    <el-form-item label="单人项目分数" prop="fenshuSingle">
      <el-input v-model="dataForm.fenshuSingle" placeholder="单人项目分数"></el-input>
    </el-form-item>
    <el-form-item label="多人项目总分数" prop="fenshuGroup">
      <el-input v-model="dataForm.fenshuGroup" placeholder="多人项目总分数"></el-input>
    </el-form-item>
    <el-form-item label="多人项目主持分数" prop="fenshuZhuchi">
      <el-input v-model="dataForm.fenshuZhuchi" placeholder="多人项目主持分数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          useType: '',
          renType: '',
          parentId: '',
          level: '',
          fenshuSingle: '',
          fenshuGroup: '',
          fenshuZhuchi: ''
        },
        dataRule: {
          name: [
            { required: true, message: '项目名称不能为空', trigger: 'blur' }
          ],
          useType: [
            { required: true, message: '是否为可申请项目0：不可申请  1：可申请不能为空', trigger: 'blur' }
          ],
          renType: [
            { required: true, message: '申请类型0单人1多人不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父级id不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '层级不能为空', trigger: 'blur' }
          ],
          fenshuSingle: [
            { required: true, message: '单人项目分数不能为空', trigger: 'blur' }
          ],
          fenshuGroup: [
            { required: true, message: '多人项目总分数不能为空', trigger: 'blur' }
          ],
          fenshuZhuchi: [
            { required: true, message: '多人项目主持分数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/stu/xiangmutypelevel/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.xiangmuTypeLevel.name
                this.dataForm.useType = data.xiangmuTypeLevel.useType
                this.dataForm.renType = data.xiangmuTypeLevel.renType
                this.dataForm.parentId = data.xiangmuTypeLevel.parentId
                this.dataForm.level = data.xiangmuTypeLevel.level
                this.dataForm.fenshuSingle = data.xiangmuTypeLevel.fenshuSingle
                this.dataForm.fenshuGroup = data.xiangmuTypeLevel.fenshuGroup
                this.dataForm.fenshuZhuchi = data.xiangmuTypeLevel.fenshuZhuchi
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/stu/xiangmutypelevel/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'useType': this.dataForm.useType,
                'renType': this.dataForm.renType,
                'parentId': this.dataForm.parentId,
                'level': this.dataForm.level,
                'fenshuSingle': this.dataForm.fenshuSingle,
                'fenshuGroup': this.dataForm.fenshuGroup,
                'fenshuZhuchi': this.dataForm.fenshuZhuchi
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
